<?php
/* @var $this UserController */
/* @var $model User */
?>

<h1>Sync Tool</h1>
<?php 
function t($data,$exit=1) {
	echo '<pre style="text-align:left;">';
	print_r($data);
	echo '</pre>';
	if($exit)exit;
}
$users = Yii::app()->db->createCommand()
		    ->select('email')
		    ->from('dtb_user u')
		
		    ->where('status=:id', array(':id'=>1))
		    ->queryColumn();
		    
		//t($users);    
?>

<div> Đồng bộ dữ liệu mailchimp và website: <input id="syncbutton" type="button" value="Đồng bộ" onclick="syncSubmit()"/></div>
<div id="console"></div>
<script>
function syncSubmit(){
	$('#console').html("");
	$('#syncbutton').val("Xin chờ...");
	$('#syncbutton').attr("disabled","disabled");
	$.ajax({
		  url: "<?php echo $this->createAbsoluteUrl('user/dosync') ?>",
		  
		});

	if(typeof(EventSource)!=="undefined")
	  {
		var objDiv = document.getElementById("console");
	  var source=new EventSource("<?php echo $this->createAbsoluteUrl('user/showconsole') ?>");
	  source.onmessage=function(event)
	    {
		    
		    if(event.data!=undefined )
		    {
	    		document.getElementById("console").innerHTML=event.data;
	    		objDiv.scrollTop = objDiv.scrollHeight;
	    		//check done
	    		if(event.data.indexOf('ALL DONE!!!')>0)
	    		{
			    	$('#syncbutton').val("Đồng bộ");
			 	    $('#syncbutton').removeAttr("disabled");
			 	   	event.target.close();
	    		}
		    }
	    
		   
	    };
	  }
	else
	  {
	  document.getElementById("console").innerHTML="Sorry, your browser does not support server-sent events...";
	  }
}





</script>